<?php
class DailyDeals extends Model_Abstract
{
	public function getAllDailyDealsOffers() {
		$sql = ' SELECT * FROM dailydeals_offer WHERE deleted=0 ';
		try {
			return $this->_connection->query($sql);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getDailyDealsOfferById ($id) {
		$sql = 'SELECT * FROM dailydeals_offer WHERE id=? AND deleted=0';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function addDailyDealsOffer($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate) {
		$sql = ' INSERT INTO dailydeals_offer SET site_id=?, partner_name=?, title_text=?, describe_text=?, credit=?, picture1_url=?, picture2_url=?, link_url=?, start_date=?, end_date=?, update_time=? ';
		try {
			return $this->_connection->execute($sql, array($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate, time()));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function updateDailyDealsOffer($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate, $id) {
		$sql = 'UPDATE dailydeals_offer SET site_id=?, partner_name=?, title_text=?, describe_text=?, credit=?, picture1_url=?, picture2_url=?, link_url=?, start_date=?, end_date=?, update_time=? WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($siteId, $PartnerName, $titleText, $describeText, $credit, $picture1Url, $picture2Url, $linkUrl, $startDate, $endDate, time(), $id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function deleteDailyDealsOffer($id) {
		$sql = 'UPDATE dailydeals_offer SET deleted=1 WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function pauseDailyDealsOffer($id) {
		$sql = 'UPDATE dailydeals_offer SET pause=(pause+1)%2 WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}

}